package ThreadDemo;

import java.util.Random;

class myThread {
    int OddSum = 0;//奇数
    int EvenSum = 0;//偶数
    public void setOddSum(long num) {
        synchronized (this) {
            OddSum += num;
        }
    }

    public void setEvenSum(long num) {
        synchronized (this) {
            EvenSum += num;
        }
    }

    public long allTotal() {
        System.out.println("奇数和: " + this.OddSum);
        System.out.println("偶数和: " + this.EvenSum);
        return this.EvenSum + this.OddSum;
    }
}
class myTest{
    int count = 0;
    public void count() {
        synchronized(this) {
            count++;
        }
    }
}
public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(()-> {
            System.out.println("Hello thread1");

        });
        Thread thread2 = new Thread(()-> {
            System.out.println("Hello thread2");
        });
        thread2.start();
        thread1.start();
        thread1.join();
        System.out.println(thread1.getState());
        System.out.println(thread2.getState());


    }
    public static void main4(String[] args) {
        Thread thread = new Thread(()-> {
            for (int i = 0; i < 1000; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        for (int i = 0; i < 1000; i++) {
            System.out.println(thread.getState());
        }
    }
    public static void main3(String[] args) throws InterruptedException {
        myTest myTest = new myTest();
        Thread thread1 = new Thread(()-> {
            for (int i = 0; i < 100000; i++) {
                myTest.count();
            }
        });
        Thread thread2 = new Thread(()-> {
            for (int i = 0; i < 100000; i++) {
                myTest.count();
            }
        });
        thread1.start();
        thread2.start();
        System.out.println(thread1.getState());
        System.out.println(thread2.getState());
    }
    public static void main2(String[] args) throws InterruptedException {
        long begin = System.currentTimeMillis();
        myThread myThread = new myThread();//实例化一个自定义类myThread
        int []array = new int[1000_0000];//1000万个元素的数组
        Random random = new Random();//实例化Random类
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100)+1;
        }
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < array.length; i++) {
                if (i % 2 == 0) {
                    myThread.setEvenSum(array[i]);
                }
            }
        });

        Thread thread2 = new Thread(()-> {
            for (int i = 0; i < array.length; i++) {
                if (i % 2 != 0) {
                    myThread.setOddSum(array[i]);
                }
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        long end = System.currentTimeMillis();
        System.out.println("总和为: " + myThread.allTotal());
        System.out.println("花费时间:  " + (end-begin));
    }

/*    public static void main(String[] args) throws InterruptedException {
        myThread myThread = new myThread();
        Thread thread1 = new Thread(()->{
            for (int i = 0; i <10000 ; i++) {
                myThread.run();
            }
        });
        Thread thread2 = new Thread(()->{
            for (int i = 0; i <10000 ; i++) {
                myThread.run();
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(myThread.getCount());
    }*/
}
